package cn.edu.thsis.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.thsis.pojo.mess;
import cn.edu.thsis.pojo.newsinfo;
import cn.edu.thsis.pojo.student;
import cn.edu.thsis.pojo.teacher;
import cn.edu.thsis.service.NewsInfoService;
import cn.edu.thsis.service.impl.NewsInfoServiceImpl;

/**
 * Servlet implementation class NewsInfoServlet
 */
@WebServlet("/NewsInfo")
public class NewsInfoServlet extends HttpServlet {
	NewsInfoService ni = new NewsInfoServiceImpl();
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;utf-8");
		String oper = req.getParameter("oper");
		String id = req.getParameter("id");
		if("newsall".equals(oper)) {//所有公告列表
			try {
				newsinfoall(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();}
			}
		else if ("newdel".equals(oper)) {//删除公告
				String nid = req.getParameter("nid");
				try {
					ni.deleteNewsInfo(nid);
					req.getRequestDispatcher("/NewsInfo?oper=newsall").forward(req, resp);
					}catch (SQLException e) {
					e.printStackTrace();
				}
			}
		else if ("newadd".equals(oper)) {//添加公告
			newadd(req,resp);}
		else if ("newstu".equals(oper)) {//学生公告列表
			try {
				nesstu(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();}
			}
		else if ("newcont".equals(oper)) {//公告详情
			String nid = req.getParameter("nid");
			newcont(req,resp,nid);
		}
		else if("news".equals(oper)) {
			String nid = req.getParameter("nid");
			news(req,resp,nid);
		}
		else if ("message".equals(oper)) {//留言
			addmes(req,resp);}
		}
	
	//留言
	private void addmes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String sid = req.getParameter("sid");
		String tid = req.getParameter("tid");
		String mid = req.getParameter("mid");
		mess mes = new mess();
		mes.setSid(sid);
		mes.setTid(tid);
		mes.setMid(mid);
		this.ni.messService(mes);
		req.getRequestDispatcher("/StuSvlt?oper=allthes").forward(req, resp);
	}
	//公告详情
	private void news(HttpServletRequest req, HttpServletResponse resp, String nid) throws ServletException, IOException {
		newsinfo news = ni.newcontService(nid);
		req.setAttribute("news", news);
		req.getRequestDispatcher("admin/admin_article.jsp").forward(req, resp);
	}
	private void newcont(HttpServletRequest req, HttpServletResponse resp, String nid) throws ServletException, IOException {
		newsinfo news = ni.newcontService(nid);
		req.setAttribute("news", news);
		req.getRequestDispatcher("student/article.jsp").forward(req, resp);
	}
	//学生公告列表
	private void nesstu(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
		List<newsinfo> niList = ni.finNewsInfoAll();
		req.setAttribute("niList", niList);
		req.getRequestDispatcher("student/home_news.jsp").forward(req, resp);
	}
	//添加公告
	private void newadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String nid = req.getParameter("nid");
		String title = req.getParameter("title");
		String summary = req.getParameter("summary");
		String author = req.getParameter("author");
		String content = req.getParameter("content");
		newsinfo ni = new newsinfo();
		ni.setNid(nid);
		ni.setTitle(title);
		ni.setSummary(summary);
		ni.setAuthor(author);
		ni.setContent(content);
		this.ni.insertNewsInfo(ni);
		req.getRequestDispatcher("/NewsInfo?oper=newsall").forward(req, resp);
	}
	
	//所有公告列表
	private void newsinfoall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<newsinfo> niList = ni.finNewsInfoAll();
		req.setAttribute("niList", niList);
		req.getRequestDispatcher("admin/admin_home.jsp").forward(req, resp);
	}

}
